<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从命令行参数列表中获取选项</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.getopt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">getopt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">getopt</span> &mdash; <span class="dc-title">从命令行参数列表中获取选项</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.getopt-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>getopt</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$options</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$longopts</code></span>
      [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$optind</code></span>
  ]] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   解析传入脚本的选项。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.getopt-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <span class="simpara">
       该字符串中的每个字符会被当做选项字符，匹配传入脚本的选项以单个连字符(-)开头。
      </span>
      <span class="simpara">
       比如，一个选项字符串 <em>&quot;x&quot;</em> 识别了一个选项 <em>-x</em>。
      </span>
      <span class="simpara">
       只允许 a-z、A-Z 和 0-9。
      </span>
     </dd>

    
    
     <dt>
<code class="parameter">longopts</code></dt>

     <dd>

      <span class="simpara">
       选项数组。此数组中的每个元素会被作为选项字符串，匹配了以两个连字符(<em>--</em>)传入到脚本的选项。
      </span>
      <span class="simpara">
       例如，长选项元素 <em>&quot;opt&quot;</em> 识别了一个选项 <em>--opt</em>。
      </span>
     </dd>

    
    
     <dt>
<code class="parameter">optind</code></dt>

     <dd>

      <span class="simpara">
       If the <code class="parameter">optind</code> parameter is present, then the
       index where argument parsing stopped will be written to this variable.
      </span>
     </dd>

    
   </dl>

  </p>
  <p class="para">
   <code class="parameter">options</code> 可能包含了以下元素：
   <ul class="simplelist">
    <li class="member">单独的字符（不接受值）</li>
    <li class="member">后面跟随冒号的字符（此选项需要值）</li>
    <li class="member">后面跟随两个冒号的字符（此选项的值可选）</li>
   </ul>
   选项的值是字符串后的第一个参数。如果需要一个值，它不介意值之前是否有前置的空格，参见以下内容。
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     选项的值不接受空格（<em>&quot; &quot;</em>）作为分隔符。
    </span>
   </p></blockquote>
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <code class="parameter">options</code> 和 <code class="parameter">longopts</code> 的格式几乎是一样的，唯一的不同之处是 <code class="parameter">longopts</code> 需要是选项的数组（每个元素为一个选项），而 <code class="parameter">options</code> 需要一个字符串（每个字符是个选项）。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.getopt-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   此函数会返回选项/参数对， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    选项的解析会终止于找到的第一个非选项，之后的任何东西都会被丢弃。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.getopt-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        添加 <code class="parameter">optind</code> 参数。
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        支持 &quot;=&quot; 作为 参数和值的分隔符。
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        增加了可选值的支持（用&quot;::&quot;指定）。
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        参数 <code class="parameter">longopts</code> 在所有系统平台上均可用。
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        此函数不再依赖于操作系统，现在也能够在 Windows 上运行。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.getopt-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-507">
    <p><strong>Example #1 <span class="function"><strong>getopt()</strong></span> 例子：基本用法</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Script&nbsp;example.php<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">"f:hp:"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -fvalue -h</pre>
</div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;f&quot;]=&gt;
  string(5) &quot;value&quot;
  [&quot;h&quot;]=&gt;
  bool(false)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-508">
    <p><strong>Example #2 <span class="function"><strong>getopt()</strong></span> 例子：引入长选项</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Script&nbsp;example.php<br /></span><span style="color: #0000BB">$shortopts&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$shortopts&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"f:"</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Required&nbsp;value<br /></span><span style="color: #0000BB">$shortopts&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"v::"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Optional&nbsp;value<br /></span><span style="color: #0000BB">$shortopts&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;These&nbsp;options&nbsp;do&nbsp;not&nbsp;accept&nbsp;values<br /><br /></span><span style="color: #0000BB">$longopts&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"required:"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Required&nbsp;value<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"optional::"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Optional&nbsp;value<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"option"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;No&nbsp;value<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"opt"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;No&nbsp;value<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$shortopts</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$longopts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -f &quot;value for f&quot; -v -a --required value --optional=&quot;optional value&quot; --option</pre>
</div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(6) {
  [&quot;f&quot;]=&gt;
  string(11) &quot;value for f&quot;
  [&quot;v&quot;]=&gt;
  bool(false)
  [&quot;a&quot;]=&gt;
  bool(false)
  [&quot;required&quot;]=&gt;
  string(5) &quot;value&quot;
  [&quot;optional&quot;]=&gt;
  string(14) &quot;optional value&quot;
  [&quot;option&quot;]=&gt;
  bool(false)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-509">
    <p><strong>Example #3 <span class="function"><strong>getopt()</strong></span> 例子：传递同一多个选项</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Script&nbsp;example.php<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -aaac</pre>
</div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;a&quot;]=&gt;
  array(3) {
    [0]=&gt;
    bool(false)
    [1]=&gt;
    bool(false)
    [2]=&gt;
    bool(false)
  }
  [&quot;c&quot;]=&gt;
  bool(false)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-510">
    <p><strong>Example #4 <span class="function"><strong>getopt()</strong></span> 例子：使用 <code class="parameter">optind</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Script&nbsp;example.php<br /></span><span style="color: #0000BB">$optind&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">'a:b:'</span><span style="color: #007700">,&nbsp;[],&nbsp;</span><span style="color: #0000BB">$optind</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pos_args&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$optind</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$pos_args</span><span style="color: #007700">);</span>
</span>
</pre></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -a 1 -b 2 -- test</pre>
</div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [0]=&gt;
  string(4) &quot;test&quot;
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.getopt-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="reserved.variables.argv.html" class="link"><var class="varname"><var class="varname"><a href="reserved.variables.argv.html" class="classname">$argv</a></var></var></a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>